
#include <stdint.h>
#include <stdio.h>

#include "soc.h"
#include "dw_i2c.h"


int iic_case1( uintptr_t pbase )
{
    dw_iic_regs_t * piic = (dw_iic_regs_t *)pbase;
    uint32_t temp;
    
    /**/
    dw_iic_init( piic, 300000 );
    
    /**/
    dw_iic_set_master_mode( piic );
    dw_iic_set_transfer_speed_fast( piic );
    dw_iic_set_target_address( piic, 0xD0 );
    
    /**/
    dw_iic_clear_all_irq( piic );
    dw_iic_enable( piic );
    
    /* write */
    piic->IC_DATA_CMD = 0x00;
    piic->IC_DATA_CMD = 0x03;
    piic->IC_DATA_CMD = 0x07;
    piic->IC_DATA_CMD = 0x0E;
    piic->IC_DATA_CMD = 0x211;
    

    /* wait */
    while ( 1 ) {
        if ( piic->IC_RAW_INTR_STAT & DW_IIC_RAW_TX_ABRT ) {
            break;
        }

        if ( piic->IC_TXFLR == 0 ) {
            break;
        }
    }
    
    if ( piic->IC_RAW_INTR_STAT & DW_IIC_RAW_TX_ABRT ) {
        temp = piic->IC_TX_ABRT_SOURCE;
        printf( "tx fail, %08lx\n", temp );

        /**/
        dw_iic_disable( piic );
        return (int)__LINE__;
    }

    /* finish, deinit */
    dw_iic_disable( piic );
    return 0;
}



int iic_case2( uintptr_t pbase )
{
    dw_iic_regs_t * piic = (dw_iic_regs_t *)pbase;
    uint32_t temp;
    int  i;
    
    /**/
    dw_iic_init( piic, 300000 );
    
    /**/
    dw_iic_set_master_mode( piic );
    dw_iic_set_transfer_speed_fast( piic );
    dw_iic_set_target_address( piic, 0x50 );
    
    /**/
    dw_iic_clear_all_irq( piic );
    dw_iic_enable( piic );
    
    /* write */
    piic->IC_DATA_CMD = 0x00;
    
    /* wait */
    while ( 1 ) {
        if ( piic->IC_RAW_INTR_STAT & DW_IIC_RAW_TX_ABRT ) {
            break;
        }

        if ( piic->IC_TXFLR == 0 ) {
            break;
        }
    }
    
    if ( piic->IC_RAW_INTR_STAT & DW_IIC_RAW_TX_ABRT ) {
        temp = piic->IC_TX_ABRT_SOURCE;
        printf( "tx fail, %08lx\n", temp );

        /**/
        dw_iic_disable( piic );
        return (int)__LINE__;
    }
    
    /* restart + read, read, read, read + stop */
    piic->IC_DATA_CMD = 0x500;
    piic->IC_DATA_CMD = 0x100;
    piic->IC_DATA_CMD = 0x100;
    piic->IC_DATA_CMD = 0x300;
    
    /* wait */
    while ( 1 ) {
        if ( piic->IC_RXFLR == 4 ) {
            break;
        }
    }
    
    /**/
    for ( i=0; i<4; i++ ) {
        temp = piic->IC_DATA_CMD;
        printf("%02lx, ", temp );
    }
    
    printf( "\n" );
    return 0;
}





// #include "umsg.h"


// void test_util_ret( umsg_t * pmsg, int iret )
// {
//     umsg_reset_ofs( pmsg );
//     umsg_add_i32( pmsg, iret );
//     umsg_send( pmsg );
//     return;
// }

// /* 
//  * switch, run test case 
//  */
// int test_case_run( umsg_t * pmsg )
// {
//     int iret;

//     switch ( pmsg->type ) {

//     case 1:
//         iret = iic_case1( DW_I2C_BASE );
//         test_util_ret( pmsg, iret );
//         break;

//     case 2:
//         iret = iic_case2( DW_I2C_BASE );
//         test_util_ret( pmsg, iret );
//         break;

//     default:
//         iret = -111;
//         test_util_ret( pmsg, iret );
//         break;      
//     }
    
//     return 0;
// }


